package com.kahriboo.log4mon.server;

import java.net.ServerSocket;
import java.net.Socket;

public class ConnectionListener extends Thread {

	private static final ConnectionListener instance = new ConnectionListener();

	private boolean isListening = false;

	private ConnectionListener() {
	}

	public static ConnectionListener getInstance() {
		return instance;
	}

	public void startListener() {
		if (!isListening) {
			isListening = true;
			super.start();
		}
	}

	public void stopListener() {
		isListening = false;

	}

	@Override
	public void run() {
		try {
			System.out.println("Launching Server !!!");

			final ServerSocket server = new ServerSocket(3105);

			while (isListening) {
				final Socket socket = server.accept();
				AppenderListener.runListener(socket);
			}

		} catch (final Exception e) {
			e.printStackTrace();
		}
	}
}
